package cn.changein.framework.service.setting.customer.impl;

import cn.changein.framework.base.pojo.page.PageFactory;
import cn.changein.framework.base.pojo.page.PageInfo;
import cn.changein.framework.kernel.model.response.ResponseData;
import cn.changein.framework.model.setting.customer.entity.CustomerService;
import cn.changein.framework.model.setting.customer.mapper.CustomerServiceMapper;
import cn.changein.framework.model.setting.customer.model.params.CustomerServiceParam;
import cn.changein.framework.model.setting.customer.model.result.CustomerServiceResult;
import cn.changein.framework.service.setting.customer.CustomerServiceService;
import cn.changein.framework.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 客服管理 服务实现类
 * </p>
 *

 * @Date 2021-02-22
 * @Copyright: companyName
 * @Version 1.0
 * @Since JDK1.8
 */
@Service
public class CustomerServiceServiceImpl extends ServiceImpl<CustomerServiceMapper, CustomerService> implements CustomerServiceService {

    @Override
    public void add(CustomerServiceParam param) {
        CustomerService entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(CustomerServiceParam param) {
        this.removeById(getKey(param));
    }

    @Override
    public void update(CustomerServiceParam param) {
        CustomerService oldEntity = getOldEntity(param);
        CustomerService newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public CustomerServiceResult findBySpec(CustomerServiceParam param) {
        return null;
    }

    @Override
    public List<CustomerServiceResult> findListBySpec(CustomerServiceParam param) {
        return null;
    }

    @Override
    public PageInfo findPageBySpec(CustomerServiceParam param) {
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return PageFactory.createPageInfo(page);
    }

    private Serializable getKey(CustomerServiceParam param) {
        return param.getId();
    }

    private Page getPageContext() {
        return PageFactory.defaultPage();
    }

    private CustomerService getOldEntity(CustomerServiceParam param) {
        return this.getById(getKey(param));
    }

    private CustomerService getEntity(CustomerServiceParam param) {
        CustomerService entity = new CustomerService();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }


    /**
     * @Description: 客服管理信息
     * @Date:2021/2/26 14:50
     * @Param: []

     */
    @Override
    public ResponseData findCustomerServiceInfo() {
        CustomerServiceResult customerServiceResultList = this.baseMapper.findCustomerServiceInfo();
        return ResponseData.success(customerServiceResultList);
    }
}
